package com.hxkj.service.article.impl;


import com.hxkj.client.service.UniversalServiceClient;
import com.hxkj.domain.enums.TagsExceptionEnum;
import com.hxkj.domain.enums.UniversalExceptionEnum;
import com.hxkj.domain.po.SiteUniversal;
import com.hxkj.service.article.UniversalService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 合作公司实现类
 * <p>
 * Created by yaosy on 2018年6月8日13:58:01
 */
@Service
@Slf4j
public class UniversalServiceImpl implements UniversalService {


    @Autowired
    private UniversalServiceClient universalServiceClient;

    @Value("${cms.realPath}")
    private String IP;


    @Override
    public SiteUniversal get(SiteUniversal siteUniversal)throws Exception {
        Long websiteId = siteUniversal.getSiteId();
        if(websiteId==null){
            throw new CustomException(UniversalExceptionEnum.UNIVERSAL_SITE_NOT_MSG);
        }
        ResultDataStruct<SiteUniversal> resultDataStruct = universalServiceClient.get(siteUniversal);
        if(resultDataStruct.getCode() != 200){
            throw new CustomMessageException(resultDataStruct.getCode(),resultDataStruct.getMessage());
        }
        siteUniversal = resultDataStruct.getData();
        siteUniversal.setIP(IP);
        return siteUniversal;
    }


    @Override
    public void modify(SiteUniversal siteUniversal)throws Exception {
        Long websiteId = siteUniversal.getSiteId();
        if(websiteId==null){
            throw new CustomException(UniversalExceptionEnum.UNIVERSAL_SITE_NOT_MSG);
        }
        if (siteUniversal.getTagsTempletId()==null){
            throw new CustomException(TagsExceptionEnum.TAGS_TAGTEMPLET_NOT_NULL);
        }
        throwException(siteUniversal);
        ResultMessageStruct resultMessageStruct =  universalServiceClient.modify(siteUniversal);
        if(resultMessageStruct.getCode() != 200){
            throw new CustomMessageException(resultMessageStruct.getCode(),resultMessageStruct.getMessage());
        }
    }

    protected void throwException(SiteUniversal siteUniversal) throws CustomException {
        /*if(siteUniversal.getId()==null){
            throw new CustomException(UniversalExceptionEnum.UNIVERSAL_NOT_ID);
        }*/
        if(StringUtils.isEmpty(siteUniversal.getWebsiteName())){
            throw new CustomException(UniversalExceptionEnum.UNIVERSAL_NAME_NOT_MSG);
        }
        if(StringUtils.isEmpty(siteUniversal.getWebsiteList())){
            throw new CustomException(UniversalExceptionEnum.UNIVERSAL_NOT_MSG);
        }
        if(StringUtils.isEmpty(siteUniversal.getVersioninfo())){
            throw new CustomException(UniversalExceptionEnum.UNIVERSAL_VERSION_INFO_ERROR);
        }
    }

}
